package com.gxa.k12.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author：Win
 * @Date: 2021/7/16 17:11
 * @Description: 解决跨域请求拦截
 */
@Configuration
public class GlobalCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource resource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        /*配置跨域*/
        corsConfiguration.addAllowedHeader("*");//允许什么样的请求头
        corsConfiguration.addAllowedMethod("*");//允许什么样的请求方法
        corsConfiguration.addAllowedOrigin("*");//允许请求路径
        corsConfiguration.setAllowCredentials(true);//允许携带cookie
        resource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(resource);
    }
}
